package com.fish.anne.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.fish.anne.service.OrderLogManager;
import com.fish.anne.model.OrderLog;
import com.fish.anne.webapp.action.BaseAction;

import java.util.List;

public class OrderLogAction extends BaseAction implements Preparable {
    private OrderLogManager orderLogManager;
    private List orderLogs;
    private OrderLog orderLog;
    private Long  orderLogId;

    public void setOrderLogManager(OrderLogManager orderLogManager) {
        this.orderLogManager = orderLogManager;
    }

    public List getOrderLogs() {
        return orderLogs;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String orderLogId = getRequest().getParameter("orderLog.orderLogId");
            if (orderLogId != null && !orderLogId.equals("")) {
                orderLog = orderLogManager.get(new Long(orderLogId));
            }
        }
    }

    public String list() {
        orderLogs = orderLogManager.getAll();
        return SUCCESS;
    }

    public void setOrderLogId(Long  orderLogId) {
        this. orderLogId =  orderLogId;
    }

    public OrderLog getOrderLog() {
        return orderLog;
    }

    public void setOrderLog(OrderLog orderLog) {
        this.orderLog = orderLog;
    }

    public String delete() {
        orderLogManager.remove(orderLog.getOrderLogId());
        saveMessage(getText("orderLog.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (orderLogId != null) {
            orderLog = orderLogManager.get(orderLogId);
        } else {
            orderLog = new OrderLog();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (orderLog.getOrderLogId() == null);

        orderLogManager.save(orderLog);

        String key = (isNew) ? "orderLog.added" : "orderLog.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}